package com.mengze.linlee_iot_adnroid.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mengze.linlee_iot_adnroid.R

class DeviceAdapter(
    private val devices: List<String>,
    private val onItemClick: (String) -> Unit) :
    RecyclerView.Adapter<DeviceAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView = view.findViewById(R.id.deviceName)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.blue_device_item, parent, false)
        println("onCreateViewHolder~~~~~")
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        println("onBindViewHolder~~~~~")
        val item = devices[position]
        holder.textView.text = devices[position]
        holder.itemView.setOnClickListener { onItemClick(item) }
    }

    override fun getItemCount() = devices.size
}
